package com.cn.service.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.dao.customer.CustomerDao;
import com.cn.entity.TbCustomer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 公司顾客service层实现
 * @Date 2021/6/4 11:37
 * @Package com.cn.service.customer
 * @Version 1.0
 */

@Service
public class CustomerServiceImpl implements CustomerService{

    @Autowired
    private CustomerDao customerDao;

    @Override
    public Page<TbCustomer> allTbCustomer(String name, String idCard, Long companyId, Integer pageNum) {
        QueryWrapper<TbCustomer> wrapper = new QueryWrapper<>();
        if(name!=null){
            wrapper.like("name",name);
        }
        if(idCard!=null&&!"".equals(idCard)){
            wrapper.like("idCard",idCard);
        }
        if(companyId!=null){
            wrapper.eq("companyId",companyId);
        }
        Page<TbCustomer> page = new Page<>(pageNum,10);
        return customerDao.selectPage(page,wrapper);
    }

    @Override
    public int addTbCustomer(TbCustomer tbCustomer) {
        return customerDao.insert(tbCustomer);
    }

    @Override
    public TbCustomer queryById(Long id) {
        TbCustomer tbCustomer = customerDao.selectById(id);
        return tbCustomer;
    }

    @Override
    public int updateTbCustomer(TbCustomer tbCustomer) {
        return customerDao.updateById(tbCustomer);
    }

    @Override
    public int delById(Long id) {
        return customerDao.deleteById(id);
    }

    @Override
    public String selectByIdCard(Long idCard) {
        QueryWrapper<TbCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name")
                .eq("idCard",idCard);
        return customerDao.selectOne(queryWrapper).getName();
    }
}
